<?php

namespace Endor\EndorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Endor\EndorBundle\Entity\Proveedores;
use Endor\EndorBundle\Form\ProveedoresType;

/**
 * Proveedores controller.
 *
 */
class ProveedoresController extends Controller
{
    private function DeepLoad(&$entity) {
        $entity = new Compras();
        $em = new \Doctrine\ORM\EntityManager();
        $em = $this->getDoctrine()->getEntityManager();
       
        $items = $em->getRepository('EndorBundle:ItemsCompras')->findByCompra($entity);
        $entity->setEstadoCompra($items);
    }
    
    /**
     * Lists all Proveedores entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entities = $em->getRepository('EndorBundle:Proveedores')->findAll();

        return $this->render('EndorBundle:Proveedores:index.html.twig', array(
            'entities' => $entities
        ));
    }

    /**
     * Finds and displays a Proveedores entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('EndorBundle:Proveedores')->find($id);
        
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Proveedores entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('EndorBundle:Proveedores:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),

        ));
    }
    
    public function showListAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();
        
        $proveedor = $em->getRepository('EndorBundle:Proveedores')->find($id);
        $entities = $em->getRepository('EndorBundle:Precios')->findBy(array(
            'Proveedor' => $id
        ));

        return $this->render('EndorBundle:Proveedores:showlist.html.twig', array(
            'entities'      => $entities,
            'proveedor' => $proveedor
        ));
    }

    /**
     * Displays a form to create a new Proveedores entity.
     *
     */
    public function newAction()
    {
        $entity = new Proveedores();
        $form   = $this->createForm(new ProveedoresType(), $entity);

        return $this->render('EndorBundle:Proveedores:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Creates a new Proveedores entity.
     *
     */
    public function createAction()
    {
        $entity  = new Proveedores();
        $request = $this->getRequest();
        $form    = $this->createForm(new ProveedoresType(), $entity);
        $form->bindRequest($request);

        /*$array = array();
        $array = $entity->getItemsCompras();
        $entity->ClearItemsCompras();*/
        
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            
            /*if(!$array->isEmpty()){          
                foreach ($array as $item) {
                    $item->setCompra($entity);
                }
                
                $em->persist($array);
            }*/
            
            $em->flush();

            return $this->redirect($this->generateUrl('proveedores_show', array('id' => $entity->getId())));
            
        }

        return $this->render('EndorBundle:Proveedores:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

     /**
     * Creates a new ItemsCompras entity.
     *
     */
    public function createItemAction()
    {
        $entity  = new ItemsCompras();
        $request = $this->getRequest();
        $form    = $this->createForm(new ComprasType(), $entity);//Definir Type
        $form->bindRequest($request);
        
        
        
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('compras_show', array('id' => $entity->getCompra()->getId())));
            
        }

        return $this->render('EndorBundle:Compras:new.html.twig', array(
            'entity' => $entity->getCompra(),
            'form'   => $form->createView()
        ));
    }
    
    /**
     * Displays a form to edit an existing Proveedores entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('EndorBundle:Proveedores')->find($id);
        
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Proveedores entity.');
        }

        $editForm = $this->createForm(new ProveedoresType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('EndorBundle:Proveedores:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing Proveedores entity.
     *
     */
    public function updateAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('EndorBundle:Proveedores')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Proveedores entity.');
        }

        $editForm   = $this->createForm(new ProveedoresType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('proveedores'));
        }

        return $this->render('EndorBundle:Proveedores:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing Items Compras entity.
     *
     */
    public function updateItemAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        /* @var $entity ItemsCompras */
        $entity = $em->getRepository('EndorBundle:ItemsCompras')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Compras entity.');
        }

        $editForm   = $this->createForm(new ComprasType(), $entity->getCompra());//Cambiar Type
        $deleteForm = $this->createDeleteForm($entity->getCompra()->getId());

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl(''));
        }

        
        //Cambiar esto
        return $this->render('EndorBundle:Compras:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    
    /**
     * Deletes a Proveedores entity.
     *
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('EndorBundle:Proveedores')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Proveedores entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('proveedores'));
    }

    /**
     * Deletes a ItemsCompras entity.
     *
     */
    public function deleteItemAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('EndorBundle:ItemsCompras')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Compras entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('compras'));
    }
    
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
